package com.prig.base.controller;

import com.prig.base.DTO.input.RolePermissionDto;
import com.prig.base.DTO.input.RolePermissionsDto;
import com.prig.base.DTO.output.RolePermissionsOutDto;
import com.prig.base.entity.Permission;
import com.prig.base.entity.RolePermissionRelationId;
import com.prig.base.service.PermissionService;
import com.prig.base.service.RolePermissionService;
import com.prig.base.util.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(path = "/rolePermission")
@Api(value = "角色权限关联")
public class RolePermissionController {

    private PermissionService permissionService;
    @Autowired
    public void setPermissionService(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    private RolePermissionService rolePermissionService;
    @Autowired
    public void setRolePermissionService(RolePermissionService rolePermissionService) {
        this.rolePermissionService = rolePermissionService;
    }

    @RequestMapping(path = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "添加角色权限关联",notes = "添加角色权限关联")
    @RequiresPermissions("rolePermission:add")
    public Result add(@RequestBody @Valid RolePermissionDto dto){
        rolePermissionService.add(new RolePermissionRelationId(dto.getPermissionId(),dto.getRoleId()));
        return new Result();
    }
    @RequestMapping(path = "/delete",method = RequestMethod.POST)
    @ApiOperation(value = "删除角色权限关联",notes = "删除角色权限关联")
    @RequiresPermissions("rolePermission:delete")
    public Result delete(@RequestBody @Valid RolePermissionDto dto){
        rolePermissionService.delete(new RolePermissionRelationId(dto.getPermissionId(),dto.getRoleId()));
        return new Result();
    }

    @GetMapping(path = "/{roleId}")
    @RequiresPermissions("rolePermission:get")
    public Result getRolePermissions(
            @PathVariable(name="roleId") long roleId
    ){
        List<Permission> all = permissionService.getPermissions();
        all = permissionService.setChildren(all);
        List<Permission> exist = permissionService.getPermissions(roleId+"");
        return new Result<>(new RolePermissionsOutDto(all,exist,roleId));
    }
    @PostMapping(path = "/{roleId}")
    @RequiresPermissions("rolePermission:update")
    public Result updateRolePermissions(
            @PathVariable(name="roleId") long roleId,
            @RequestBody RolePermissionsDto dto
    ){
        String[] idsStr = dto.getStrIds().split(",");
        List<RolePermissionRelationId> list = Arrays.stream(idsStr)
                .map(str->new RolePermissionRelationId(Long.parseLong(str),roleId))
                .collect(Collectors.toList());
        rolePermissionService.delete(roleId);
        rolePermissionService.add(list);
        return new Result();
    }
}
